using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Animator))]
public class AnimationController  : MonoBehaviour
{
    #region Variables
    Animator _mAnimator;

    [Header("Animation Properties")]
    [Tooltip("��������޸�")]
    public AnimatorOverrideController animationStyle;
    RuntimeAnimatorController _mAnimationOrigin;

    [Header("Animation Set Properties")]
    [Tooltip("����״̬�л�Ԥ�裬�����������ò㼶Ϊ���б�����")]
    public List<AnimationObject> animationSets;

    [Header("Animation Match Target Set Properties")]
    [Tooltip("���ö���λ��ƥ��Ԥ�裬�����������ò㼶Ϊ���б�����")]
    public List<AnimationMatchTargetObject> animationMatchSet;

    Dictionary<int, AnimationProcesser> _mProcessersDic = new Dictionary<int, AnimationProcesser>();
    #endregion

    #region BuiltIn Methods
    void Awake()
    {
        InitAnimationController();
    }

    void Update()
    {
        UpdateAnimationProcess();
        UpdateAnimationStyle();
    }
    #endregion

    #region Init
    void InitAnimationController()
    {
        _mAnimator = GetComponent<Animator>();
        _mAnimationOrigin = _mAnimator.runtimeAnimatorController;

        // ��ʼ�����ɴ����ű�
        for(int i = 0; i < animationSets.Count; i++) 
        {
            AnimationProcesser tmpProcesser = new AnimationProcesser();
            // ע�����ű�
            tmpProcesser.Init();
            _mProcessersDic.Add(i, tmpProcesser);
            tmpProcesser.MainRegister(_mAnimator, animationSets[i], i);
        }

        for(int i = 0; i < animationMatchSet.Count; i++)
        {
            // ע���Ҫ�ű�
            if(animationMatchSet[i])
                _mProcessersDic[i].MatchTargetRegister(animationMatchSet[i]);
        }
    }
    #endregion

    #region Update Animation Process
    void UpdateAnimationProcess()
    {
        for(int i = 0; i < _mProcessersDic.Count; i++) 
        {
            _mProcessersDic[i].ProcessAnimation();
            _mProcessersDic[i].MatchAnimaionTarget();
        }
    }

    void UpdateAnimationStyle()
    {
        if (animationStyle)
            _mAnimator.runtimeAnimatorController = animationStyle;
        else
            _mAnimator.runtimeAnimatorController = _mAnimationOrigin;
    }
    #endregion

    #region Play Methods
    /// <summary>
    /// ����״̬����(Update)
    /// </summary>
    /// <param name="animationName">��������</param>
    /// <param name="layer">�����㼶</param>
    public void PlayStateAnimation(string animationName, int layer)
    {
        _mProcessersDic[layer].PlayStateAnimation(animationName);
    }

    /// <summary>
    /// ���Ų��붯��(����ı�״̬����)(Trigger)
    /// </summary>
    /// <param name="animationName">��������</param>
    /// <param name="layer">�����㼶</param>
    public void PlayPlayableAnimation(string animationName, int layer)
    {
        _mProcessersDic[layer].PlayPlayableAnimation(animationName);
    }

    /// <summary>
    /// ʹ�ö���λ��ƥ��
    /// </summary>
    /// <param name="animationName">��������</param>
    /// <param name="targetPostion">����ƥ��λ������</param>
    /// <param name="targetRotation">����ƥ��λ����ת</param>
    /// <param name="layer">�����㼶</param>
    public void SetTargetMatch(string animationName, Vector3 targetPostion, Quaternion targetRotation, int layer)
    {
        _mProcessersDic[layer].SetTargetMatch(animationName, targetPostion, targetRotation);
    }
    #endregion

    #region Animation Parameter Handle Methods
    
    #endregion

    #region Animator Values
    public Vector3 AnimatorDeltaPosition
    {
        get { return _mAnimator.deltaPosition; }
    }

    public Quaternion AnimatorDeltaRotation
    {
        get { return _mAnimator.deltaRotation; }
    }

    public Vector3 AnimatorVelocity
    {
        get { return _mAnimator.velocity; }
    }
    #endregion
}
